
jQuery.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();

    jQuery.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }

            o[this.name].push(this.value || "");
        } else {
            o[this.name] = this.value || "";
        }
    });

    return o;
};


jQuery.loadingRequestCount = 0;

jQuery.showAjaxLoadingSplash = function() {
    jQuery.loadingRequestCount = (jQuery.loadingRequestCount < 0) ? 0 : jQuery.loadingRequestCount;

    if (jQuery.loadingRequestCount == 0) {
        $("#loading-frame", top.document).show();
        $("#loading-window", top.document).show();
    }

    jQuery.loadingRequestCount++;
};

jQuery.hideAjaxLoadingSplash = function() {
    jQuery.loadingRequestCount--;

    if (jQuery.loadingRequestCount == 0) {
        $("#loading-frame", top.document).hide();
        $("#loading-window", top.document).hide();
    }
};

jQuery.invokeGET = function(_url, _data, _success, _error) {
    jQuery.__invoke("GET", _url, _data, _success, _error);
};

jQuery.invokePUT = function(_url, _data, _success, _error) {
    jQuery.__invoke("PUT", _url, _data, _success, _error);
};

jQuery.invokeDELETE = function(_url, _data, _success, _error) {
    jQuery.__invoke("DELETE", _url, _data, _success, _error);
};

jQuery.invokePOST = function(_url, _data, _success, _error) {
    jQuery.__invoke("POST", _url, _data, _success, _error);
};

jQuery.__invoke = function(_method, _url, _data, _success, _error) {
    if (_data != null && _data != undefined) {
        if (_method == "GET" || _method == "DELETE") {
            if (_data.serialize) {
                _data = _data.serialize();
//                alert("_data.serialize() \n" +_data);
            }
        } else {
            if (_data.serializeObject) {
                _data = jQuery.toJSON(_data.serializeObject());
//                alert("jQuery.toJSON(_data.serializeObject() \n" + _data);
            } else {
                _data = jQuery.toJSON(_data);
//                alert("jQuery.toJSON(_data) \n" + _data);
            }
        }
    } else {
        _data = null;
    }
    
    jQuery.ajax({
        type:     _method,
        dataType: "json",
        contentType: "application/json",
        url:      _url,
        data:     _data,

        beforeSend: function() {
            jQuery.showAjaxLoadingSplash();
        },
        success: function(data) {
            jQuery.hideAjaxLoadingSplash();
      
                // jQuery.notifySuccess(data._BARD_success);

                if (_success != undefined && _success instanceof Function) {
                    try {
                    	//alert("success SUCCESS");
                        _success(data);
                    } catch (e) {
                    	alert("success ERROR ++ " + e);
                        //logger.debug(e);
                        // ignore
                    }
                }
      
        },
        error: function(data, status, errMsg) {
            jQuery.hideAjaxLoadingSplash();

            alert(data.status + " - " + errMsg);

            if (_error != undefined && _error instanceof Function) {
                try {
                    _error(data, status, errMsg);
                } catch (e) {
                	alert("error ERROR ++ " + e);
                    //logger.debug(e);
                    // ignore
                }
            }
        }
    });
};